Database connection pools are a great way of providing secure, authenticated connections
to a database for multiple users and to the MySQL Connector Python API provides a useful method for developing connection pools in the form of the MySQL
Connection Pool module. In this video, you'll learn how to create a
connection pool for a database using the MySQL
Connection Pool module. Little Lemon need
to provide secure, authenticated connections
to their database for their team of
web developers. They've decided that the
most efficient way of providing these connections is through connection pooling. Let's take a few minutes to find out how to build a
connection pool. Then use your new skills to help Little Lemon build a
secure connection pool. A database connection pool
is managed and maintained using a module called
MySQL Connection Pool. The module is held in a directory called
mysql.connector.pooling. You can import the module into your working
environment and access its functionality
using the import syntax. The from keyword is used to identify the modules location. In other words, the code is
instructing Python to access the sub-directory and return the MySQL Connection
Pool library. The MySQL Connection
Pool module has many useful functions and attributes that you
can make use of. Let's look at a few of these. The pool_name class
attribute is used to identify the name of the pool to be used
in the connection. If you don't specify a pool then one is automatically
generated instead. You can create as many
pools as you need. The pool_size attribute states the number of connections that have been created for a pool. The default number of
connections is five, but you can create up to 32 connections for
a single pool. Finally, there's the
connection_ID attributes. This is a unique ID assigned to each
connection in the pool. There are also
many class methods available in the MySQL
Connection Pool module. You can use the get
connection method to request a connection. The pool then assigns a free connection if
one is available. If no connection is available, then you'll receive a pool
exhausted error Instead. The is connected method is a Boolean function that returns either a true or false value depending on whether a
connection has been made. This is a useful way
of avoiding errors. Finally, the close
method informs the connection pool that a user has completed
their session. The user no longer needs the connection so the
connection can be placed back into the pool as an available connection for
any new users who need it. Now that you're familiar with
the module connection pool, let's see if you can
help Little Lemon. As you learned earlier, Little Lemon want to create
a connection pool to provide users with efficient
access to their database. Before you can create
a connection to, you first need to import the MySQL Connection
Pool library using the MySQL
Connector Python API. Once you've imported
the connector, the next step is to
make a connection to the database called the
pool, Little Lemon pool. Then use the pool size attribute to specify
for connections. Use the local host as your host and place the pool on the
Little Lemon database, then type the username
and password. All this code is passed as
arguments or parameters to the MySQL Connection Pool module and assigned to the pool. Next, you need to create a Python list of users
for the connection pool. You can call this list users populate the list with members of Little Lemon's guest list. You then need to create
a SQL select statement. This statement must
accept an integer. The integer must correspond with different ID requests accessing different data points
as database users. Now you need to
use the four-loop. You can use it with
the range function. The range function is used
with the pool size attribute. This means that no matter
how big the pool is, the loop always runs to the end. The next step is to set up
the applications connection. Write a statement that checks if a connection was successfully
made within the pool. This statement also avoids any errors appearing
in your coach. Then write a statement
that instantiates a new cursor from the existing
act of pool connections. This action must occur for each new life connection that successfully made with the pool. Next, create a print statement that displays the following
information on screen. The user requesting
information from the database, the unique connection
assigned to this user and the unique booking ID
that they're requesting. The print statement is
formatted using curly braces. These symbols take the
specified variables in the order that
they are specified from the format function. This means that
the information is printed in the
order you specify. The next line of your code is a parameterized
select statement. This statement uses the initial SQL select
statement from earlier and combines it with the
incremented ID to assign a different
booking ID to each user. Then you can use the fetch all methods to return
all information that corresponds with this
SQL select statement and print the
information on screen. You can also create an else
statement that generates an error message on-screen if a live connection
cannot be found. Finally, use the close
method to return a user's connection back to the pool when they have
ended their session. You should now know how to create a connection
pool for a database using the functions
and attributes available with a MySQL
Connection Pool module.